|
|
|
|
|
|
|
The output of the log file can be viewed with a text editor, as shown in Figure 21.5. |
|
|
|
|
|
|
|
|
Figure 21.5
With a few lines of code, you can
add a log file to your application. |
|
|
|
|
|
|
|
|
Recall from Listing 21.1 (in the preceding section) that you opened the file in input mode by using the keyword Input. To write data, you open the file for sequential output. However, instead of using the keyword Output, you use Append. Compare the following two lines of code, each of which opens a file for output: |
|
|
|
|
|
|
|
|
'Append mode adds to an existing file or creates a new one
Open "ErrorLog.TXT" for Append as #1
'Output Mode always creates a new file, erases any existing information
Open "ErrorLog.TXT" for Output as #1 |
|
|
|
|
|
|
|
|
Append mode means data written to the file is added to the end of any existing data. This is perfect for the log file application because you want to keep adding to the log file. Opening a file for Output means that any existing data will be erased. In either case, the Open statement automatically creates the file if it does not exist. After a file has been opened for Output, you can use a couple of different statements to write information to it. The Print# and Write# statements, described in the next section, provide different formatting options for sequential files. |
|
|
|
|
|
|
|
|
The Print# statement works almost exactly like the Print method described in Chapter 20, "Accessing the Windows API," except instead of going to an onscreen object, the output is routed to the open file. Unless a semicolon or other separator is at the end of the list of things to be printed, a new line is automatically inserted into the file after each print. The syntax of the Print# statement is |
|
|
|
|
|
|
|
|
Print #filenum,expressions |
|
|
|
|
|
|
|
|
Another statement, the Write# statement, works like the Print# statement but automatically adds separators and delimiters. The syntax of the Write# statement is |
|
|
|
|
|
|
|
|
Write #filenum,expressions |
|
|
|
|
|
|
|
|
The Write statement is intended for use with the Input# statement, described earlier in the "Reading from a Sequential Text File" section. Some examples of both statements are listed here, and the resulting file is shown in Figure 21.6. |
|
|
|
|
|
|
|
|
Print #1, "This is an example of Print# and Write#"
Print #1, "Siler","Brian"
Write #1, "Siler","Brian"
Print #1, "Really?", 2*3;Spc(5);"Good.";
Write #1,"Date",1/1/1998,1000*5;
Print #1, vbCrLf & "Bye!" |
|
|
|
|
|